<?php 
    
	function showvar($hVar)
	{	
		echo '<pre>';
		
		if ( !isset($hVar) ) echo 'undefined';
		else var_dump($hVar);
		
		echo '</pre> ------------------------------------- <br/>';
	}
	
	function printvar($hVar)
	{
		echo '<pre>';
		
		if ( !isset($hVar) ) echo 'undefined';
		else print_r($hVar);
		
		echo '</pre> ------------------------------------- <br/>';
	}

    /* debuging & logger class */
    class CDebug 
    {
		var $m_hEntryTable;
		
		 static function &GetInstance()
		 {
			global $g_hObjects;
			static $m_hApp = NULL;
						 
			if(!isset($m_hApp))
			{
				$m_hApp = new CDebug();					 
				$g_hObjects[] = $m_hApp;
				$m_hApp->OnInit();
			}
			
			return $m_hApp;
		}
		
		
		function OnInit()
		{
			$this->m_hEntryTable = array();
		}
		
		function OnDestroy()
		{
			$this->SaveToFile();
			$this->Display();
		}
		
		function IsActive()
		{
			if(DEBUG_ACTIVE == true)
			{
			return true;
			}
			
			return false;
		}
		
		function Add($szMsg)
		{
			if(DEBUG_ACTIVE == false)
				return;
			
			$szTime = date("d-m-Y H:i:s");
			
			$szBacktrace = debug_backtrace();

			array_shift($szBacktrace);

			$szBacktrace = array_shift($szBacktrace);
			
			if( strlen($szBacktrace['class']) > 0 && strlen($szBacktrace['function']) > 0 )
			{
				$szCaller = $szBacktrace['class'].'::'.$szBacktrace['function'].'( )';
			}
			else if( strlen($szBacktrace['class']) <= 0 && strlen($szBacktrace['function']) > 0 )
			{
				$szCaller = $szBacktrace['function'].'( )';
			}
			else if( strlen($szBacktrace['class']) <= 0 && strlen($szBacktrace['function']) <= 0 )
			{
				$szCaller = $szBacktrace['file'].' ';	
			}
				
			$this->m_hEntryTable[] = array("time" => $szTime,"caller" => $szCaller,"msg" => $szMsg);
		}
		
		/* save debug output to file */
		function SaveToFile()
		{
			if(DEBUG_ACTIVE == false)
			return;
		
			$szMsg = '';
		
			$hFile = @fopen(LOG_DIR.'/debug.log','a+');
			
			if(!$hFile)
			{
				dir("CDebug - can't access debug.log!");
				return false;
			}
			
			foreach($this->m_hEntryTable as $n => $hTable)
			{
				$szMsg.=$hTable['time'].' '.$hTable['caller'] .' '.$hTable['msg']."\n";
			}
			
			fwrite($hFile,$szMsg);
			fclose($hFile);
		}
							
		/* print debug output */
		function Display()
		{
			foreach($this->m_hEntryTable as $n => $hTable)
			{
			echo $hTable['time'].' '.$hTable['caller'] .' '.$hTable['msg']."<br>";
			}
		
		}						 
    }
    
?>